package fudan.se.project.service.infrastructure.entity;


import fudan.se.project.domain.User;
import fudan.se.project.domain.conference.Conference;
import fudan.se.project.repository.entity.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {


    private final UserRepository userRepository;
    private PasswordEncoder passwordEncoder;

    @Autowired
    public UserService(UserRepository userRepository, PasswordEncoder encoder) {
        this.userRepository = userRepository;
        this.passwordEncoder = encoder;
    }


    public User save(User user) {
        return userRepository.save(user);
    }

    public boolean isUserExistent(String username) {
        return (userRepository.findByUsername(username) != null);
    }

    public String encode(String password) {
        return passwordEncoder.encode(password);
    }

    public boolean isCorrectPassword(User user, String password) {
        return passwordEncoder.matches(password, user.getPassword());
    }

    //<editor-fold desc="entity query methods">

    public User findByUsername(String username) {
        return userRepository.findByUsername(username);
    }

    public User findByEmail(String email) {
        return userRepository.findByEmail(email);
    }

    public List<User> findByRealname(String realname) {

        return userRepository.findByRealname(realname);
    }

    public List<User> findPcMemberCandidates(String realname, Conference conference) {
        return userRepository.findUsersNotPcMemberInConference(realname, conference);
    }

    //</editor-fold>


}
